// pages/my/stockManage/stockManage.js
var app = getApp()
Page({
  data: {
    topNav:'',
    selectIndex: 0,
    isUpload:true,
    goodsList: [],
    leftBtn:[],
    agentId:'',
    goodsType:'',
    isMgAdd:false,
    isStock:false,
    isChangePrice:false,
    ifJoin:true,
    trash:false,
    noData:false
  },
  onLoad: function(options) {
      if (options.type==0) {          //我的库存
        this.setData({
          isUpload:false,
          ifJoin:true,
          isStock:true,
          isMgAdd:true
        })
        wx.setNavigationBarTitle({
          title: '我的库存'
        })
      }else if (options.type==1) {    //我的商品
        this.setData({
          ifJoin:false,
          trash:true
        })
        wx.setNavigationBarTitle({
          title: '我的商品'
        })
      }else{                          //修改价格
        this.setData({
          isUpload:false,
          ifJoin:false,
          isStock:true,
          isChangePrice:true
        })
        wx.setNavigationBarTitle({
          title: '修改价格'
        })
      }
      app.getMemInfo().then(res=>{
        console.log(res.data.agentId)
        let agentId = res.data.agentId
        this.setData({
          agentId
        })
        this.getProductType(agentId)
      })
    },
  //获取商品类型列表
  getProductType(agentId){
    var that = this
    var leftBtn = this.data.leftBtn
    let data = {
      agentId,
      requestType:'client'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      let btnList = res.data.product_types
      //默认种类
      that.setData({
        goodsType:res.data.product_types[0].id
      })
      btnList.forEach(item=>{
        leftBtn.push({name:item.name,id:item.id})
        that.setData({
          leftBtn
        })
      })
      that.getGoodsList()
    })
  },
  //获取商品列表
  getGoodsList(){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      goodsList:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      searchKey:''
    }
    let goodsList = []
    app.request('wx_app/mall/client/list',data,'GET').then(res=>{
      console.log(res)
      if(res.code==1){      
        res.data.pageContent.forEach(item=>{
          goodsList.push({
            name:item.name,
            price:item.stockPrice,
            url:item.url,
            id:item.id,
            stock:item.stock
          })
          that.setData({
            goodsList
          })
        })
        if(that.data.goodsList.length==0){
          that.setData({
            noData:true
          })
        }else{
          that.setData({
            noData:false
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  // comeStockTap: function(e) {
  //   console.log(e.currentTarget.dataset.id)
  //   wx.navigateTo({
  //     url: '../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
  //   })
  // },
  addnewGoodsTap: function() {
    wx.navigateTo({
      url: '../addGoods/addGoods',
    })
  },
  bindStockGoodSet(e){
    wx.navigateTo({
      url: '../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type+'&price='+e.currentTarget.dataset.price
    })
  },
  addUnderLineGoods(e){
    wx.navigateTo({
      url:'../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
    })
  },
  selecttypeTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    let type = dataSet.type
    var selectIndex = dataSet.index;
    var categoryId = dataSet.categoryId;//查询不同分类下的数据
    this.setData({
      selectIndex: selectIndex,
      goodsType:type
    })
    this.getGoodsList(type)
  },
  jumpAddGoods(){
    wx.navigateTo({
      url:'../addCustomGoods/addCustomGoods'
    })
  },
  bindTopNav(e){
    console.log(e.currentTarget.dataset.index)
    let topNav = e.currentTarget.dataset.index
    this.setData({
      topNav
    })
    if (topNav==0) {
      this.setData({
        isUpload:true,
        isMgAdd:false,
        isStock:false,
        isChangePrice:false
      })
    }else{
      this.setData({
        isUpload:false,
        isMgAdd:false,
        isStock:true,
        isChangePrice:true
      })
    }
  },
  fromLibrary(){
    wx.navigateTo({
      url:'../library/library'
    })
  },
  addStock(){
    wx.navigateTo({
      url:'../addStock/addStock'
    })
  },
  bindDelete(e){
    let data = {
      productId:e.currentTarget.dataset.id,
      productType:this.data.goodsType,
      agentId:this.data.agentId
    }
    var that = this
    wx.showModal({
      title:'删除商品',
      content:'删除商品后，对应库存信息也被删除！',
      confirmColor:'#539FFF',
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          app.request('wx_app/agent/stock/del',data,'GET').then(res=>{
            console.log(res)
            if(res.code==1){
              that.getGoodsList()
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')

        }
      }
    })
    
    console.log(data)
    
  }
})